package org.grow.jssp.service;

import org.grow.jssp.entity.JsspFile;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: xwg
 * @CreateDate: 2022/3/14
 */

@Service
public class FileOps implements FileOperator{

    @Override
    public List<JsspFile> ops(String filename){
        File file = new File(filename);
//
        File[] oldList = file.listFiles();
//        过滤掉文件夹
        List<File> newList = new ArrayList<File>();
        for (int i = 0; i < oldList.length; i++) {
            File item = oldList[i];
            if (item.isFile()) {
                newList.add(item);
            }
        }
//         ["a.txt","b.txt"]
//        ["{title:"a.txt", size:10MB }","{title:"a.txt", size:2MB }"]
//        System.out.println(Arrays.toString(list));  映射f(x)
        ArrayList<JsspFile> finalList = new ArrayList<>();
        for (int i = 0; i < newList.size(); i++) {
            File item = newList.get(i);
            JsspFile jsspFile = new JsspFile();
            jsspFile.setTitle(item.getName());
            jsspFile.setSize((int)item.length());
            finalList.add(jsspFile);
        }
        System.out.println(newList);
        return finalList;
    }
}
